package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.UserDao;
import dao.Factoty.DaoFactory;
import domain.User;
import service.Factory.ServiceFactory;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User();
		
		user.setUsername(request.getParameter("username"));
		user.setPassword(request.getParameter("password"));
		user.setGender(request.getParameter("sex"));
		user.setEmail(request.getParameter("email"));
		user.setTelephone(request.getParameter("telephone"));
		user.setIntroduce(request.getParameter("profile"));
		user.setRole("user");
		
		if (!user.getUsername().equals("") && !user.getPassword().equals("")) {
			try {
				if (ServiceFactory.getUserServiceInstance().register(user)) {
					response.sendRedirect(request.getContextPath()+ "/client/registersuccess.jsp");
				} else {
					response.sendRedirect(request.getContextPath()+ "/client/register.jsp");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.sendRedirect(request.getContextPath()+ "/client/register.jsp");
			}
		} else {
			response.sendRedirect(request.getContextPath()+ "/client/register.jsp");
		}
	}

}
